package net.zjitc.jdbc.book;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @Author admin
 * @Date 2019/4/26 8:15
 * 测试类（界面）
 */
public class Test {
    static BookDaoDB dao = new BookDaoDB();
    static Scanner scanner = new Scanner(System.in);
    static Scanner scannerDouble = new Scanner(System.in);
    static Scanner scannerInt = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("欢迎进入图书管理系统：");
            System.out.println("1--保存图书信息");
            System.out.println("2--查询图书信息");
            System.out.println("3--更新图书信息");
            System.out.println("4--删除图书信息");
            System.out.println("5--退出");
            System.out.println("请输入你的选择（1-5）：");
            int choose = scannerInt.nextInt();
            switch (choose) {
                case 1:
                    insertUI();
                    break;
                case 2:
                    selectUI();
                    break;
                case 3:
                    updateUI();
                    break;
                case 4:
                    deleteUI();
                    break;
                case 5:
                    exitUI();
                    break;
                default:
                    System.out.println("输入有误，请输入1-5：");
            }
        }
    }

    private static void exitUI() {
        /* 调用工具类方法去完成关闭数据库连接等对象*/
        dao.exit();
        System.out.println("退出系统");
        System.exit(0);
    }

    private static void insertUI() {
        System.out.println("-----添加界面------");
        System.out.println("请输入图书名称：");
        String name = scanner.nextLine();
        System.out.println("请输入出版社：");
        String press = scanner.nextLine();
        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入价格：");
        double price = scannerDouble.nextDouble();
        Book book = new Book(name, press, author, price);
        /* 调用工具类的添加方法，将book对象保存在数据库*/
        int c = dao.insert(book);
        if (c > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("图书名称重复，添加失败");
        }
    }

    private static void selectUI() {
        System.out.println("-----所有图书信息如下：-------");
        System.out.println("编号\t出版社\t作者\t价格");
//        dao.select();
        /* 查询返回，返回保存图书对象的集合*/
        ArrayList<Book> arrayList = dao.selectAll();
        /* 遍历集合，展示图书信息*/
        Iterator<Book> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Book book = iterator.next();
            System.out.println(book.getId()+"\t"+book.getName()
            +"\t"+book.getPress()+"\t"+book.getAuthor()+"\t"
            +book.getPrice());
        }
    }

    private static void updateUI() {
        System.out.println("-----更新界面-----");
        System.out.println("请输入要修改的图书名称：");
        String name = scanner.nextLine();
        Book book = dao.query(name);
        if (book == null) {
            System.out.println("你要修改的图书不存在");
        } else {
            System.out.println("请输入要修改的图书名称");
            String n = scanner.nextLine();
            System.out.println("请输入要修改的出版社：");
            String press = scanner.nextLine();
            System.out.println("请输入要修改的作者：");
            String author = scanner.nextLine();
            System.out.println("请输入要修改的价格：");
            double price = scannerDouble.nextDouble();
            Book b = new Book(book.getId(), n, press, author, price);
            int c = dao.update(b);
            if (c > 0) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
    }

    private static void deleteUI() {
        System.out.println("-----删除界面-----");
        System.out.println("请输入要删除的图书名称：");
        String name = scanner.nextLine();
        Book book = dao.query(name);
        if (book == null) {
            System.out.println("你要删除的图书不存在");
        } else {
            int c=dao.delete(book);
            if (c > 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
    }
}
